package com.io;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Scanner;

public class PipedInputOutputStreamDemo01 {
    public static void main(String[] args) throws IOException {
        PipedInputStream pipedInputStream = new PipedInputStream();
        PipedOutputStream pipedOutputStream = new PipedOutputStream();
        try {
            pipedOutputStream.connect(pipedInputStream);
            Scanner scanner = new Scanner(System.in);

            byte[] buff = new byte[1024];
            int len = -1;
            while (true) {
                System.out.println("请输入要向管道中输出的数据");
                String msg = scanner.nextLine();
                pipedOutputStream.write(msg.getBytes());
                len = pipedInputStream.read(buff);
                if (msg.equals("886"))
                    break;
                String s = new String(buff,0,len);
                System.out.println("s = " + s);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            pipedOutputStream.close();
            pipedInputStream.close();
        }
    }
}
